﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Excel = Microsoft.Office.Interop.Excel; 

namespace QuanLyCongDan.GUI
{
    public partial class frmTimKiemCongDan : DevExpress.XtraEditors.XtraForm
    {
        private QuanLyCongDan.DTO.DTO_CongDan congDanSearchCondition = new QuanLyCongDan.DTO.DTO_CongDan();
        private System.Data.OleDb.OleDbCommand comm = new System.Data.OleDb.OleDbCommand(); 
        public frmTimKiemCongDan()
        {
            InitializeComponent();
        }

        private void simpleButtonXacNhan_Click(object sender, EventArgs e)
        {
            textEditGiaTri.Text = textEditGiaTri.Text.Trim();
            if (comboBoxEditTieuChi.SelectedText.Equals("Năm sinh") == true)
            {
                if (string.IsNullOrEmpty(textEditGiaTri.Text))
                {
                    MessageBox.Show("Xin hãy nhập vào năm sinh");
                    return;
                }

                foreach (char curChar in textEditGiaTri.Text)
                {
                    if (Char.IsDigit(curChar) == false)
                    {
                        MessageBox.Show("Xin hãy nhập vào năm sinh bằng số");
                        return;
                    }
                }
                congDanSearchCondition.NamSinh = Int32.Parse(textEditGiaTri.Text);
            }
            switch (comboBoxEditTieuChi.SelectedText)
            {
                case "Tên":
                    congDanSearchCondition.Ten = textEditGiaTri.Text;
                    break;
                case "Học vấn":
                    congDanSearchCondition.HocVan = textEditGiaTri.Text;
                    break;
                case "Dân tộc":
                    congDanSearchCondition.DanToc = textEditGiaTri.Text;
                    break;
                case "Tôn giáo":
                    congDanSearchCondition.TonGiao = textEditGiaTri.Text;
                    break;
                case "Thường trú":
                    congDanSearchCondition.ThuongTru = textEditGiaTri.Text;
                    break;
                case "Họ Tên Cha Mẹ":
                    congDanSearchCondition.HoTenChaMe = textEditGiaTri.Text;
                    break;
                case "Đơn Vị":
                    congDanSearchCondition.DonVi = textEditGiaTri.Text;
                    break;
                default:
                    break;
            }
        }

        private void comboBoxEditTieuChi_SelectedIndexChanged(object sender, EventArgs e)
        {
            textEditGiaTri.Text = string.Empty;
            switch (comboBoxEditTieuChi.SelectedText)
            {
                case "Tên":
                    textEditGiaTri.Text = congDanSearchCondition.Ten;
                    break;
                case "Học vấn":
                    textEditGiaTri.Text = congDanSearchCondition.HocVan;
                    break;
                case "Dân tộc":
                    textEditGiaTri.Text = congDanSearchCondition.DanToc;
                    break;
                case "Tôn giáo":
                    textEditGiaTri.Text = congDanSearchCondition.TonGiao;
                    break;
                case "Thường trú":
                    textEditGiaTri.Text = congDanSearchCondition.ThuongTru;
                    break;
                case "Họ Tên Cha Mẹ":
                    textEditGiaTri.Text = congDanSearchCondition.HoTenChaMe;
                    break;
                case "Đơn Vị":
                    textEditGiaTri.Text = congDanSearchCondition.DonVi;
                    break;
                case "Năm sinh":
                    textEditGiaTri.Text = congDanSearchCondition.NamSinh.ToString();
                    break;
                default:
                    break;
            }
        }

        private void simpleButtonXoa_Click(object sender, EventArgs e)
        {
            textEditGiaTri.Text = string.Empty;
            switch (comboBoxEditTieuChi.SelectedText)
            {
                case "Tên":
                    congDanSearchCondition.Ten = string.Empty;
                    break;
                case "Học vấn":
                    congDanSearchCondition.HocVan = string.Empty;
                    break;
                case "Dân tộc":
                    congDanSearchCondition.DanToc = string.Empty;
                    break;
                case "Tôn giáo":
                    congDanSearchCondition.TonGiao = string.Empty;
                    break;
                case "Thường trú":
                    congDanSearchCondition.ThuongTru = string.Empty;
                    break;
                case "Họ Tên Cha Mẹ":
                    congDanSearchCondition.HoTenChaMe = string.Empty;
                    break;
                case "Đơn Vị":
                    congDanSearchCondition.DonVi = string.Empty;
                    break;
                case "Năm sinh":
                    congDanSearchCondition.NamSinh = 0;
                    break;
                default:
                    break;
            }
        }

        private void simpleButtonXoaHet_Click(object sender, EventArgs e)
        {
            textEditGiaTri.Text = string.Empty;
            congDanSearchCondition = new QuanLyCongDan.DTO.DTO_CongDan();
        }

        private void simpleButtonSearch_Click(object sender, EventArgs e)
        {
            try
            {
                comm = DAL.DAL_CongDan.OleDBCommandSearchCongDan(congDanSearchCondition);
                DataSet ds = DAL.DAL_CongDan.GetCongDanTable(comm);
                dataGridViewFoundCongDan.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Chọn tên File excel sẽ lưu";
            saveFileDialog.Filter = "Excel 2003|*.xls";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                ExportData2Excel(saveFileDialog.FileName);
            }
        }

        private void ExportData2Excel(string filePath)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            int i = 0;
            int j = 0; 

            for (i = 0; i <= dataGridViewFoundCongDan.RowCount  - 1; i++)
            {
                for (j = 0; j <= dataGridViewFoundCongDan.ColumnCount  - 1; j++)
                {
                    DataGridViewCell cell = dataGridViewFoundCongDan[j, i];
                    xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                }
            }

            xlWorkBook.SaveAs(
                filePath, 
                Excel.XlFileFormat.xlWorkbookNormal, 
                misValue, 
                misValue, 
                misValue, 
                misValue, 
                Excel.XlSaveAsAccessMode.xlExclusive, 
                misValue, 
                misValue, 
                misValue, 
                misValue, 
                misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

            MessageBox.Show("Hoàn tất");
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        private void simpleButtonUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = (DataTable)dataGridViewFoundCongDan.DataSource;
                DAL.DAL_CongDan.UpdateCongDanTable(dt.DataSet);
                MessageBox.Show("Hoàn tất");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void simpleButtonNewSearch_Click(object sender, EventArgs e)
        {
            textEditGiaTri.Text = string.Empty;
            congDanSearchCondition = new QuanLyCongDan.DTO.DTO_CongDan();
            dataGridViewFoundCongDan.DataSource = null;
            dataGridViewFoundCongDan.Refresh();
        }

        

        
    }
}